package com.novel.app.view.storys.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.dongdxiang.framework.http.ImageLoader;
import com.novel.app.R;
import com.novel.app.utils.BaseViewHolder;
import com.novel.app.view.storys.bean.StoryEntity;

/**
 * Created by d_xxiang on 2016/12/13.
 */
public class StoryAdpter extends ArrayAdapter<StoryEntity.NovelEntity> {


        private LayoutInflater inflater;
        private Context context;
        public StoryAdpter(Context context) {
            super(context, android.R.layout.simple_list_item_1);
            inflater = LayoutInflater.from(context);
            this.context = context;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent){
            InfoHolder holder = null;
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_novel, null);
                holder = new InfoHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (InfoHolder) convertView.getTag();
            }
            holder.populateView(position, getItem(position));
            return convertView;
        }

        class InfoHolder extends BaseViewHolder<StoryEntity.NovelEntity> {
            private ImageView img;
            private TextView novel_name;
            private TextView novel_author;
            private RadioButton novel_type;
            private TextView instro;

            public InfoHolder(View view) {
                super(view);
                img = (ImageView) view.findViewById(R.id.img);
                novel_name = (TextView) view.findViewById(R.id.novel_name);
                novel_author = (TextView) view.findViewById(R.id.novel_author);
                novel_type = (RadioButton) view.findViewById(R.id.novel_type);
                instro = (TextView) view.findViewById(R.id.instro);
            }

            @Override
            public void populateView(int position, StoryEntity.NovelEntity item) {

                ImageLoader.disImage(context,item.getImg_url(),img);
                novel_name.setText(item.getName());
                novel_author.setText(item.getAuthor());
                if (item.getIs_finished() == 1) {
                    novel_type.setText("已完结");
                    novel_type.setChecked(false);
                }else {
                    novel_type.setText("连载中");
                    novel_type.setChecked(true);
                }
                instro.setText(item.getIntro());
            }
        }
}
